package com.example.lin.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;

/**
 * @className: ExportUtil
 * @description: 导出工具类
 * @author: Lin.Gu
 * @date: 2024/2/6
 */
@Slf4j
public class ExportUtil {

    public static String exportUrl = "D://test/";
    public static String exportSuffix = ".xlsx";

    public static void exportTheFile(String fileName, List<Object[]> data) {
        //开始写入
        XSSFWorkbook workbook = new XSSFWorkbook();
        Sheet addSheet = workbook.createSheet("Sheet1");

        // Write data rows
        for (int i = 0; i < data.size(); i++) {
            Row dataRow = addSheet.createRow(i);
            Object[] rowData = data.get(i);

            for (int j = 0; j < rowData.length; j++) {
                Cell cell = dataRow.createCell(j);
                if (rowData[j] != null) {
                    cell.setCellValue(rowData[j].toString());
                } else {
                    cell.setCellValue("");
                }
            }
        }
        try (FileOutputStream outputStream = new FileOutputStream(exportUrl.concat(fileName).concat(exportSuffix))) {
            // 将workbook写入到输出流中，从而保存为Excel文件
            workbook.write(outputStream);
            System.out.println("Excel file has been created successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭workbook以释放资源
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static void writeDataToFile(List<Object> data, String fileName) {
        try (FileWriter writer = new FileWriter(exportUrl.concat(fileName).concat(".txt"))) {
            StringBuilder line = new StringBuilder();
            for (Object row : data) {
                line.append(row.toString());
                line.append("\n"); // 每行数据后换行
            }
            writer.write(line.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void writeDataToFile(List<Object> data, String exportUrl, String fileName) {
        try (FileWriter writer = new FileWriter(exportUrl.concat(fileName).concat(".txt"))) {
            StringBuilder line = new StringBuilder();
            for (Object row : data) {
                line.append(row.toString());
                line.append("\n"); // 每行数据后换行
            }
            writer.write(line.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static <T> void exportDtoToExcel(List<T> dtoList, String fileName) {
        try (XSSFWorkbook workbook = new XSSFWorkbook();
             FileOutputStream fos = new FileOutputStream(exportUrl.concat(fileName).concat(exportSuffix))) {
            Sheet sheet = workbook.createSheet("Sheet1");
            if (!dtoList.isEmpty()) {
                Class<?> dtoClass = dtoList.get(0).getClass();
                Field[] fields = dtoClass.getDeclaredFields();
                Row headerRow = sheet.createRow(0);
                for (int i = 0; i < fields.length; i++) {
                    Cell cell = headerRow.createCell(i);
                    cell.setCellValue(fields[i].getName());
                }
                for (int i = 0; i < dtoList.size(); i++) {
                    T dto = dtoList.get(i);
                    Row dataRow = sheet.createRow(i + 1);
                    for (int j = 0; j < fields.length; j++) {
                        Field field = fields[j];
                        field.setAccessible(true);
                        Object value = field.get(dto);
                        Cell cell = dataRow.createCell(j);
                        if (value != null) {
                            cell.setCellValue(value.toString());
                        }
                    }
                }
            }
            workbook.write(fos);
            fos.close();
            workbook.close();
            log.info("导出成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出处理失败:" + e);
        }
    }

}



